package com.example.demo.controller;


import com.example.demo.base.*;
import com.example.demo.entity.Complain;
import com.example.demo.service.ComplainService;
import com.example.demo.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@Validated
@RequestMapping({"/manage"})
public class ComplainController {

    @Autowired
    private ComplainService complainService;

    @RequestMapping("/complain/list")
    public BaseResponse list(
            @RequestParam(required = false, value = "userId") String userId,
            @RequestParam(required = false, value = "state") Integer state,
            @RequestParam(required = false, value = "type") String type,
            @RequestParam(required = false, value = "page") Integer pageNo,
            @RequestParam(required = false, value = "rows") Integer pageSize,
            HttpServletRequest request){
        QueryBuilder queryBuilder = new QueryBuilder();
        if(StringUtils.isNotBlank(userId)){
            queryBuilder.addFilter(Filter.eq("user_id",userId));
        }
        if(state!=null){
            queryBuilder.addFilter(Filter.eq("state",state));
        }
        queryBuilder .addFilter(Filter.eq("type",type))
                    .addOrder(Order.desc("id"));
        Pageable<Complain> pageable = complainService.selectEntitiesByPage(queryBuilder,pageNo,pageSize);

        BaseResponse response = new BaseResponse();
        response.setRows(pageable.getRecords());
        response.setTotal(new Long(pageable.getTotal()).intValue());

        return response;
    }

    @RequestMapping("/complain/add")
    public BaseResponse add(
            @RequestParam(required = false, value = "userId") Long userId,
            @RequestParam(required = false, value = "info") String info,
            @RequestParam(required = false, value = "type") Integer type,
            HttpServletRequest request){

        Complain complain = new Complain();
        complain.setType(type);
        complain.setUserId(userId);
        complain.setInfo(info);
        complainService.insertEntity0(complain);
        return BaseResponse.success();
    }

    @RequestMapping("/complain/update")
    public BaseResponse update(
            @RequestParam(required = false, value = "id") Long id,
            @RequestParam(required = false, value = "info") String info,
            @RequestParam(required = false, value = "result") String result,
            HttpServletRequest request){

        Complain complain = new Complain();
        complain.setInfo(info);
        if(StringUtils.isNotBlank(result)){
            complain.setState(1);
            complain.setResult(result);
        }
        UpdateBuilder updateBuilder = new UpdateBuilder(complain);
        updateBuilder.addFilter(Filter.eq("id",id));
        complainService.updateEntity(updateBuilder);
        return BaseResponse.success();
    }

}
